package org.ielse.frame.view;

import org.ielse.frame.R;
import org.ielse.frame.ViewCallback;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class LeftMenuLayout extends FrameLayout implements OnClickListener {

	private ViewCallback viewCallback;

	private LinearLayout layContainer;

	public LeftMenuLayout(Context context) {
		this(context, null);
	}

	public LeftMenuLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		viewCallback = (ViewCallback) context;
		LayoutInflater.from(context).inflate(R.layout.layout_main_left_menu, this);

		layContainer = (LinearLayout) findViewById(R.id.lay_container);
		resetContentAnimation();

		findViewById(R.id.lay_auto_back).setOnClickListener(this);
		findViewById(R.id.lay_radar).setOnClickListener(this);
	}

	@Override protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		viewCallback = null;
	}

	@Override public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_auto_back:
			viewCallback.onViewCallback(ViewCallback.ACT_LEFT_MENU_AUTO_BACKUP);
			break;
		case R.id.lay_radar:
			viewCallback.onViewCallback(ViewCallback.ACT_LEFT_MENU_RADAR);
			break;
		}
	}

	public void startContentAnimation() {
		final DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
		for (int i = 0; i < layContainer.getChildCount(); i++) {
			final View child = layContainer.getChildAt(i);
			ObjectAnimator anim1 = ObjectAnimator.ofFloat(child, "translationX", -layContainer.getWidth(), 0);
			ObjectAnimator anim2 = ObjectAnimator.ofFloat(child, "alpha", 0, 1);
			AnimatorSet set = new AnimatorSet();
			set.play(anim1).with(anim2);
			set.setInterpolator(decelerateInterpolator);
			set.setDuration(320);
			set.setStartDelay(i * 100);
			set.start();
		}
	}

	public void resetContentAnimation() {
		for (int i = 0; i < layContainer.getChildCount(); i++) {
			View child = layContainer.getChildAt(i);
			child.setAlpha(0);
		}
	}

}
